package com.socialbank.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wqdata.auth.bean.Dept;
import com.wqdata.auth.dto.DeptDto;
import com.wqdata.auth.service.DeptService;
import com.wqdata.auth.util.DeptUtils;
import com.wqdata.auth.util.UserUtils;

@Controller
@RequestMapping("sys/dept")
public class DeptController {
	
	private static Logger logger = Logger.getLogger(DeptController.class);
	
	@Autowired
	private DeptService deptService;
	
	@RequestMapping("toDeptList")
	public String toRoleList(HttpServletRequest request,Model model) {
		//要对数据进行排序 前端递归处理数据
		List<Dept> sortTreeList = new ArrayList<>();
		List<Dept> deptList = deptService.getDeptListByUserId(UserUtils.getCurrrentUserId());
		//前台进行处理再展示
		//TreeUtils.sortTreeList(sortTreeList, deptList, 0L);
		DeptUtils.sortTreeList(sortTreeList, deptList, "0");
		model.addAttribute("deptList", sortTreeList);
		return "systemConfig/DeptList";
	}
	
	@RequestMapping("toDeptEdit")
	@RequiresPermissions(value = { "dept:add", "dept:update" }, logical = Logical.OR)
	public String toUserEdit(@ModelAttribute("editFlag") int editFlag,Long deptId,Long parentId,Model model){
		//1 为增加 2为修改
		if(editFlag == 2){
			DeptDto deptDto = deptService.getDeptDtoById(deptId);
			model.addAttribute("dept", deptDto);
		}
		if(editFlag == 1){
			if(parentId != null){
				DeptDto deptDto = deptService.getTreeDtoById(parentId);
				model.addAttribute("dept",deptDto);	
			}
		}
		return "systemConfig/DeptAdd";
	}
	
	@RequestMapping("/updateDept")
	@RequiresPermissions("dept:update")	
	public @ResponseBody Map<String, Object> updateDept(@RequestBody Dept dept){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			deptService.updateDept(dept);
			resultMap.put("code", 200);
			resultMap.put("message", "修改部门信息成功");
		} catch (Exception e) {
			logger.error("修改部门失败", e);
			e.printStackTrace();
		}		
		return resultMap;
	} 
	@RequestMapping("/saveDept")
	@RequiresPermissions("dept:add")	
	public @ResponseBody Map<String, Object> saveDept(@RequestBody Dept dept){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			deptService.addDept(dept);
			resultMap.put("code", 200);
			resultMap.put("message", "修改部门信息成功");
		} catch (Exception e) {
			logger.error("增加部门失败", e);
			e.printStackTrace();
		}		
		return resultMap;
	} 
	
	//获取所有树形结构 部门节点信息
	@RequestMapping("/getParentDeptTreeData")
	public @ResponseBody  List<Dept> getParentDeptTreeData(Long deptId){
		List<Dept> treeList = this.deptService.getAllDeptDto();
		if(deptId!=null){//代表进入的是修改页面
			Dept dept = deptService.getDeptById(deptId);
			//找出父节点
			String branCode = dept.getBranCode();
			Map<Long,Dept> childrenMap = new HashMap<Long,Dept>();
			//1:把他的儿子,孙子全部找出来
			DeptUtils.getAllChildrenMap(childrenMap, treeList, branCode);
			//2:把他的儿子,孙子从列表里面remove
			Iterator<Dept> treeIterator = treeList.iterator();
			while(treeIterator.hasNext()){
				Dept tree = treeIterator.next();
				//如果子节点列表里面存在这个对象,则删除
				if(childrenMap.get(tree.getId())!=null){
					treeIterator.remove();
				}
				//删除本身
				if(tree.getId().equals(deptId)){
					treeIterator.remove();
				}
			}			
		}
		return treeList;
	}
	
	@RequestMapping("/delDept")
	@RequiresPermissions("dept:delete")
	public @ResponseBody Map<String, Object> delDept(Long deptId){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			deptService.delDept(deptId);
			resultMap.put("code", 200);
			resultMap.put("message", "删除部门信息成功");
		} catch (Exception e) {
			logger.error("删除部门失败", e);
			e.printStackTrace();
		}
		return resultMap;
	}
}
